package com.caipu.controller.admin;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caipu.common.Result;
import com.caipu.dto.DishDetailDTO;
import com.caipu.dto.DishSearchDTO;
import com.caipu.entity.Dish;
import com.caipu.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/admin/dish")
@CrossOrigin
public class AdminDishController {
    
    @Autowired
    private DishService dishService;
    
    @GetMapping("/list")
    public Result<Page<DishDetailDTO>> list(DishSearchDTO searchDTO) {
        Page<DishDetailDTO> page = dishService.searchDishes(searchDTO);
        return Result.success(page);
    }
    
    @GetMapping("/{id}")
    public Result<DishDetailDTO> getById(@PathVariable Long id) {
        DishDetailDTO dish = dishService.getDishDetail(id);
        return Result.success(dish);
    }
    
    @PostMapping
    public Result<Void> save(@RequestBody Dish dish) {
        // 处理详情图片JSON
        if (dish.getDetailImages() != null && !dish.getDetailImages().trim().isEmpty()) {
            try {
                // 验证JSON格式
                JSON.parseArray(dish.getDetailImages());
            } catch (Exception e) {
                return Result.error("详情图片格式错误，请输入有效的JSON数组");
            }
        }
        
        dish.setCreateTime(LocalDateTime.now());
        dish.setUpdateTime(LocalDateTime.now());
        dish.setDeleted(0);
        
        boolean success = dishService.save(dish);
        return success ? Result.success() : Result.error("保存失败");
    }
    
    @PutMapping("/{id}")
    public Result<Void> update(@PathVariable Long id, @RequestBody Dish dish) {
        // 处理详情图片JSON
        if (dish.getDetailImages() != null && !dish.getDetailImages().trim().isEmpty()) {
            try {
                // 验证JSON格式
                JSON.parseArray(dish.getDetailImages());
            } catch (Exception e) {
                return Result.error("详情图片格式错误，请输入有效的JSON数组");
            }
        }
        
        dish.setId(id);
        dish.setUpdateTime(LocalDateTime.now());
        
        boolean success = dishService.updateById(dish);
        return success ? Result.success() : Result.error("更新失败");
    }
    
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        boolean success = dishService.removeById(id);
        return success ? Result.success() : Result.error("删除失败");
    }
    
    @PostMapping("/{id}/tags")
    public Result<Void> addTags(@PathVariable Long id, @RequestBody List<Long> tagIds) {
        dishService.addDishTags(id, tagIds);
        return Result.success();
    }
    
    @DeleteMapping("/{id}/tags")
    public Result<Void> removeTags(@PathVariable Long id, @RequestBody List<Long> tagIds) {
        dishService.removeDishTags(id, tagIds);
        return Result.success();
    }
}